<?

class Admin extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		if(!$this->auth->is_admin() && !$this->auth->is_super_admin())
			$this->auth->verify();
		$this->load->model('activities_model');
		$this->load->model('item_model');
		$this->load->model('sales_model');
	}

	public function index()
	{
		$this->db_check();
		$data['activities'] = $this->activities_model->get_activities();
		$data['sales'] = $this->sales_model->get_sales();

		$date = _get_sunday_of_the_week(date('Y-m-d'));
		$data['date'] = array('sunday' => $date->sunday, 'saturday' => $date->saturday);
		$data['period'] = array('week' => 'Week', 'month' => 'Month', 'day' => 'Day', 'all' => 'All');

		$data['page'] = 'admin/dashboard';
		$this->load->view('admin/index', $data);
	}

	public function sales_report($period='week',$start=null,$end=null)
	{
		if(!$start){
			$date = _get_sunday_of_the_week(date('Y-m-d'));
			$start = $date->sunday;
			$end = $date->saturday;
		}
		if($period == 'all'){$start = null; $end = null;}
		switch ($period) {
			case "day":
				$data['title'] = "for the ".date("jS \of F Y", strtotime($start));
				break;
			case "week":
				$data['title'] = "from ".date("F j, Y", strtotime($start))." to ".date("F j, Y", strtotime($end));
				break;
			case "month":
				$data['title'] = "for the month of ".date("F",strtotime($start));
				break;
			case "all":
				$data['title'] = null;
				break;
		}
		$data['date'] = (object) array('start' => $start, 'end' => $end);
		$data['item_total_sold'] = $this->item_model->get_items();
		foreach($data['item_total_sold']->result() as $item){
			$item_total = $this->sales_model->get_sale_total_item_by_item_id($item->item_id,$start,$end)->row();
			$item->item_sales_total = $item_total->sale_item_total;
			$item->total_quantity = $item_total->sale_item_quantity;
			$item->sale_item_discount_price = $item_total->sale_item_discount_price;
		}
		$this->load->view('admin/includes/_sales_report',$data);
	}

	public function recent_act($idx=null)
	{
		$data['activities'] = $this->activities_model->get_activities_with_idx($idx);
		$this->load->view('admin/includes/_recent_activities',$data);
	}

	public function recent_sales($idx=null)
	{
		$data['sales'] = $this->sales_model->get_sales_with_idx($idx);
		foreach($data['sales']->result() as $item){
			$sale_item = $this->sales_model->get_sale_item_by_sale_id($item->sale_id)->result();
			$item_list = '';
			foreach($sale_item as $each_item){
				$item_list .= '<b>qty:</b> '.$each_item->sale_item_quantity.' <b>name:</b> '.$each_item->item_name.' <b>discount:</b> -P '.number_format($each_item->sale_item_discount_price,2,'.',',').' <b>less:</b> '.$each_item->sale_item_discount.'% <br/>';
			}
			$item->item_list = $item_list;
		}
		$this->load->view('admin/includes/_recent_sales',$data);
	}

	public function send_email_report()
	{
		$period = $this->input->post('period');
		switch ($period) {
			case "day":
				$start = $this->input->post('general_date');
				$end = $this->input->post('general_date');
				$data['title'] = "for the ".date("jS \of F Y", strtotime($start));
				break;
			case "week":
				$start = $this->input->post('week_start_value');
				$end = $this->input->post('week_end_value');
				$data['title'] = "from ".date("F j, Y", strtotime($start))." to ".date("F j, Y", strtotime($end));
				break;
			case "month":
				$start = $this->input->post('general_date');
				$end = date('Y-m-d', strtotime('+1 month -1 days'.$start));
				$data['title'] = "for the month of ".date("F",strtotime($start));
				break;
			case "all":
				$start = null; $end = null;
				$data['title'] = null;
				break;
		}
		$data['item_total_sold'] = $this->item_model->get_items();
		foreach($data['item_total_sold']->result() as $item){
			$item_total = $this->sales_model->get_sale_total_item_by_item_id($item->item_id,$start,$end)->row();
			$item->item_sales_total = $item_total->sale_item_total;
			$item->total_quantity = $item_total->sale_item_quantity;
			$item->sale_item_discount_price = $item_total->sale_item_discount_price;}

		$message = $this->load->view('admin/includes/_sales_report',$data,true);

		send_email(
				$this->session->userdata['user_email'],
				'A Cycle House Report',
				$message
			);
	}

	public function db_check(){
		$this->activities_model->delete_act_sales(date('Y-m-d', strtotime('-1 years '.date('Y-m-d'))));
	}
}
